package com.itlong.cloud.controller.old;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldGetProjectPageDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldProjectInfoDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldUpdateProjectDTO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldProjectInfoPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.smartcard.ISmartCardOldProjectServiceClient;
import com.itlong.cloud.user.smartcard.ISmartCardOldUserInfoServiceClient;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     旧一卡通的项目模块。
 * <desc/>
 *
 * @createDate 2017/11/17.
 */
@RestController
@RequestMapping("/bas/projectInfo")
public class SmartCardOldProjectController {


      private static final Logger LOG = LoggerFactory.getLogger(SmartCardOldProjectController.class);

      @Autowired
      ISmartCardOldUserInfoServiceClient iSmartCardOldUserInfoServiceClient;

      @Autowired
      ISmartCardOldProjectServiceClient iSmartCardOldProjectServiceClient;
     /**
      * <desc>
      *      添加项目信息。
      * <desc/>
      *
      * @param  myUserName
      * @param  token
      * @param  projectJson
      * @return
      * @author shengen.T
      * @createDate 2017/11/17
      */
    @LoggerInfo(operateDesc = LogDescConstant.SAVE_OLDSMARTCARD_PEOJECTINFO_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "/manage/add",method = RequestMethod.POST)
    public Message addProjectInfo(String myUserName,String token,String projectJson){
        Message message;
        try {
            System.out.println("旧一卡通添加小区--" + projectJson);
            //判断用户名
            if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(myUserName)){
                return new Message(Code.LOGIN_ERROR);
            }
            //判断项目json数据
            List<SmartCardOldProjectInfoDTO> projectDTOS =  JSONObject.parseArray(projectJson, SmartCardOldProjectInfoDTO.class);
             if((null == projectDTOS)||(projectDTOS.size() <1)){
                  return new Message(Code.PARAM_ERR);
             }
             for(SmartCardOldProjectInfoDTO projectInfo:projectDTOS){
                 if(StringUtils.isBlank(projectInfo.getProjectId())||
                         StringUtils.isBlank(projectInfo.getProjectName())){
                     return new Message(Code.NOT_PARAM);
                 }
                 if (projectInfo.getProjectStatus() == null) {
                     projectInfo.setProjectStatus(0);
                 }
                 Integer result = iSmartCardOldProjectServiceClient.addProjectInfo(projectInfo);
                 if(result==0){
                     return new Message(Code.PROJECT_NAME_EXIST);
                 }
             }
            message = new Message(Code.SUCCESS);
        }catch (Exception e){
            throw new DataAccessException("【旧一卡通add】添加项目信息失败", e);
        }
        return message;
    }

     /**
      * <desc>
      *      旧一卡通_获取项目列表。
      * <desc/>
      *
      * @param pageDTO 获取列表请求参数
      * @return 返回列表信息
      * @author shengen.T
      * @createDate 2017/11/21
      */
    @LoggerInfo(operateDesc = "旧一卡通_获取项目列表")
    @ResponseBody
    @RequestMapping(path = "/manage/list",method = RequestMethod.POST)
    public Message getProjectList(SmartCardOldGetProjectPageDTO pageDTO){
       Message message;
       try{
           //判断用户名
           if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(pageDTO.getMyUserName())){
               return new Message(Code.LOGIN_ERROR);
           }
           Page<SmartCardOldProjectInfoPageVO> page = iSmartCardOldProjectServiceClient.getProjectInfoPage(pageDTO);
           Map<String,Object> map = new HashMap<>();
           map.put("projectInfos",page.getRows());
           map.put("totalNumber",page.getPage().getTotalNumber());
           message = new Message(Code.SUCCESS,map);
       }catch (Exception e){
           throw new DataAccessException("【旧一卡通add】添加项目信息失败", e);
       }
       return message;
    }

     /**
      * <desc>
      *      修改项目。
      * <desc/>
      *
      * @param  updateDTO
      * @return
      * @author shengen.T
      * @createDate 2017/11/21
      */
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_OLDSMARTCARD_PEOJECTINFO_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "/manage/update",method = RequestMethod.POST)
    public Message update(SmartCardOldUpdateProjectDTO updateDTO){
        Message message;
        try{
            if (StringUtils.isBlank(updateDTO.getProjectId())) {
                return new Message(Code.PARAM_ERR);
            }
            //判断用户名
            if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(updateDTO.getMyUserName())){
                return new Message(Code.LOGIN_ERROR);
            }
            Integer result = iSmartCardOldProjectServiceClient.updateProject(updateDTO);
           if(result==0){
              return new Message(Code.PROJECT_NAME_EXIST);
           }else if(result==2){
               return new Message(Code.ERROR,"不存在该项目信息");
           }
           message = new Message(Code.SUCCESS);
        }catch (Exception e){
            throw new DataAccessException("【旧一卡通update】修改项目信息失败", e);
        }
        return message;
    }

}
